<template>
  <div>
    <!-- 面包屑区域 开始 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 面包屑区域 结束 -->
    <!-- 卡片区域 开始 -->
    <el-card class="box-card">
      <!-- 搜索添加区域 开始 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" class="input-with-select" v-model="paramsData.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="$router.push('/goods/add')">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 搜索添加区域 结束 -->
      <!-- 表格区域 开始 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column
          label="商品名称"
          prop="goods_name"
          width="580"
        ></el-table-column>
        <el-table-column
          label="商品价格（元）"
          prop="goods_price"
        ></el-table-column>
        <el-table-column
          label="商品重量"
          prop="goods_weight"
          width="80"
        ></el-table-column>
        <el-table-column
          label="创建时间"
          width="120"
        >
        <template v-slot="{ row }">
          {{ row.add_time | formDate}}
        </template>
        </el-table-column>
        <el-table-column label="操作" width="190">
          <template>
            <el-button type="primary" icon="el-icon-edit" size="mini"
              >编辑</el-button
            >
            <el-button type="danger" icon="el-icon-delete" size="mini"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 表格区域 结束 -->
      <!-- 分页区域 开始 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="paramsData.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="paramsData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 分页区域 结束 -->
    </el-card>
    <!-- 卡片区域 结束 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      paramsData: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      goodsList: [],
      total: 0
    }
  },

  created() {
    this.getGoodsList()
  },

  methods: {
    async getGoodsList() {
      const res = await this.$axios.get('/goods', {
        params: this.paramsData
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange(val) {
      this.paramsData.pagesize = val
      this.getGoodsList()
    },
    handleCurrentChange(val) {
      this.paramsData.pagenum = val
      this.getGoodsList()
    }
  }
}
</script>

<style scoped lang='less'>
</style>
